using System.Runtime.CompilerServices;
using UnityEngine;

public class EconomyPlayTimeTimer : MonoBehaviour
{
	public delegate void PlayTimeTriggerEventHandler(string newTimeTrigger);

	public static string[] playTimeTriggerValues = new string[21]
	{
		"m00", "m05", "m10", "m15", "m30", "m45", "m60", "m90", "h02", "h03",
		"h04", "h05", "h07", "h10", "h15", "h20", "h30", "h45", "h60", "h80",
		"h99"
	};

	private int frameCount;

	private int frameResetCounter = 60;

	private bool running;

	private float totalPlayTime;

	private string lastTimeTrigger;

	private float lastTime;

	[method: MethodImpl(32)]
	public event PlayTimeTriggerEventHandler PlayTimeTriggerEvent;

	public static int GetTimeInSecondsForTriggerValue(string triggerValue)
	{
		if (triggerValue == string.Empty || triggerValue == string.Empty)
		{
			return 0;
		}
		char c = triggerValue[0];
		int num = 0;
		switch (c)
		{
		case 'm':
			num = 60;
			break;
		case 'h':
			num = 3600;
			break;
		}
		int result = 0;
		int.TryParse(triggerValue.Substring(1), out result);
		return num * result;
	}

	public static string GetPlayTimeFormatted(int playTimeInSeconds)
	{
		for (int i = 1; i < playTimeTriggerValues.Length; i++)
		{
			if (playTimeInSeconds >= GetTimeInSecondsForTriggerValue(playTimeTriggerValues[i - 1]) && playTimeInSeconds < GetTimeInSecondsForTriggerValue(playTimeTriggerValues[i]))
			{
				return playTimeTriggerValues[i - 1];
			}
		}
		return playTimeTriggerValues[playTimeTriggerValues.Length - 1];
	}

	private void FirePlayTimeTriggerEvent(string newTimeTrigger)
	{
		if (this.PlayTimeTriggerEvent != null)
		{
			this.PlayTimeTriggerEvent(newTimeTrigger);
		}
	}

	private void Update()
	{
		if (running)
		{
			frameCount++;
			if (frameCount >= frameResetCounter)
			{
				frameCount = 0;
				float num = Time.realtimeSinceStartup - lastTime;
				totalPlayTime += num;
				lastTime = Time.realtimeSinceStartup;
				CheckTrigger();
			}
		}
	}

	public void Init(int tPlayTime)
	{
		totalPlayTime = tPlayTime;
		lastTimeTrigger = GetPlayTimeFormatted(tPlayTime);
	}

	public void StartTimer()
	{
		Debug.Log("Starting play time timer!");
		lastTime = Time.realtimeSinceStartup;
		running = true;
	}

	public void PauseTimer()
	{
		running = false;
		float num = Time.realtimeSinceStartup - lastTime;
		totalPlayTime += num;
		lastTime = Time.realtimeSinceStartup;
		Debug.Log("Pausing play time timer! " + totalPlayTime);
		CheckTrigger();
	}

	private void CheckTrigger()
	{
		if (!(lastTimeTrigger == playTimeTriggerValues[playTimeTriggerValues.Length - 1]) && lastTimeTrigger != string.Empty)
		{
			int num = 0;
			for (num = 0; num < playTimeTriggerValues.Length && !(playTimeTriggerValues[num] == lastTimeTrigger); num++)
			{
			}
			int timeInSecondsForTriggerValue = GetTimeInSecondsForTriggerValue(playTimeTriggerValues[num + 1]);
			if ((float)timeInSecondsForTriggerValue != 0f && totalPlayTime > (float)timeInSecondsForTriggerValue)
			{
				lastTimeTrigger = playTimeTriggerValues[num + 1];
				FirePlayTimeTriggerEvent(playTimeTriggerValues[num + 1]);
			}
		}
	}

	public int GetTotalPlayTime()
	{
		return Mathf.RoundToInt(totalPlayTime);
	}

	public int GetTotalPlayTimeInMinutes()
	{
		return Mathf.RoundToInt(totalPlayTime / 60f);
	}

	public string GetLastTimeTrigger()
	{
		return lastTimeTrigger;
	}
}
